var kb = (function(kb){
  var self = kb;

  self.getLocalStorage = function(key){
    return localStorage.getItem(key);
  };

  self.setLocalStorage = function(key, value){
    if (value == null) {
      localStorage.removeItem(key);
    }
    else{
      localStorage[key] = value;
    };
  };

  return self;
}(kb || {}));

kb.hosts = (function(hosts){
  var self = hosts;

  var _localStorageKey = "Kooboo.CMS.SiteSwitcher.Hosts";
  var _hosts = null;

  self.save = function(hosts){
    kb.setLocalStorage(_localStorageKey, JSON.stringify(hosts));
  };

  self.all = function(){

    var localHosts = kb.getLocalStorage(_localStorageKey);

    if (localHosts == null) {
      return [];
    };
    
    return JSON.parse(localHosts);
  };

  self.contains = function(host){
    var hosts = self.all();

    for (var i = 0; i < hosts.length; i++) {
      if (hosts[i] == host) {
        return true;
      };
    };
    
    return false;
  };

  self.add = function(host){
    if (self.contains(host)) {
      return;
    };

    var hosts = self.all();
    hosts.push(host);

    self.save(hosts);
  };

  self.removeAll = function(){
    self.save([]);
  };

  self.remove = function(host){
    var hosts = self.all();
    var news = [];

    for (var i = 0; i < hosts.length; i++) {
      if(hosts[i] != host){
        news.push(hosts[i]);
      }; 
    };

    self.save(news);

    kb.sites.removeSites(host);
  };

  return self;
}(kb.hosts || {}));

kb.sites = (function(sites){
  var self = sites;

  var _localStorageKey = "Kooboo.CMS.SiteSwitcher.Sites";

  self.save = function(sites){
    kb.setLocalStorage(_localStorageKey, JSON.stringify(sites));
  };

  self.all = function(){

    var localSites = kb.getLocalStorage(_localStorageKey);
    if (localSites == null) {
      return [];
    };

    return JSON.parse(localSites);
  };

  self.contains = function(site){
    var sites = self.all();

    for (var i = 0; i < sites.length; i++) {
      if (sites[i].Host == site.Host && sites[i].Name == site.Name) {
        return true;
      };
    };      

    return false;
  };

  self.add = function(site){
    if (self.contains(site)) {
      return;
    };

    var sites = self.all();
    sites.push(site);

    self.save(sites);
  };

  self.getSites = function(host){
    var result = [];

    var sites = self.all();
    for (var i = 0; i < sites.length; i++) {
      if (sites[i].Host == host) {
        result.push(sites[i]);
      };
    };

    return result;
  };

  self.removeAll = function(){
    self.save([]);
  };

  self.removeSites = function(host){
    var sites = self.all();
    var news = [];

    for (var i = 0; i < sites.length; i++) {
      var site = sites[i];
      if (site.Host != host) {
        news.push(site);
      };
    };

    self.save(news);
  };

  return self;
}(kb.sites || {}));